//
//  AppendAppCell.swift
//  todoios
//
//  Created by qianli on 2024/8/29.
//

import UIKit
import SwiftyUserDefaults

class NodeItemCell: UITableViewCell {
    var icoLogo: UIImageView = UIImageView()
    var labelTitle: UILabel = UILabel()
    var labelDelaytime:UILabel = UILabel()
    var checkImgView:UIImageView = UIImageView()
    var vipLogo:UIImageView = UIImageView()
//    public var didTapAddBlock: os_block_t? //点击添加
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        GHL4Rhb91XYBer27()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func GHL4Rhb91XYBer27(){
        //row-height=80
        let conentHeight = 66.0
        let rowView = UIView(frame: CGRect(x: 15, y: 80/2-conentHeight/2, width: qianli_pm_width-30, height: conentHeight))
        //切记，此时bounds.size.width不准，如果用bounds.size.width，则要layoutSubviews进行frame更新
        self.contentView.addSubview(rowView)
        rowView.backgroundColor = .systemBackground
        rowView.layer.cornerRadius = 10
        
        self.icoLogo.frame = CGRect(x: 15, y: conentHeight/2 - 32/2, width: 48, height: 32) //240*160
        rowView.addSubview(self.icoLogo)
        self.icoLogo.image = "qianli_flag_default".qianli_R_image
        self.icoLogo.contentMode = .scaleAspectFit
        
        let btnWidth = 16.0
        let iBtnLeft = qianli_pm_width - 30 - btnWidth - 10
        self.checkImgView.frame = CGRect(x: iBtnLeft, y: conentHeight/2-btnWidth/2, width: btnWidth, height: btnWidth)
        rowView.addSubview(self.checkImgView)
        self.checkImgView.image = "qianli_checkbox_n".qianli_R_image  //100*100(3)  qianli_checkbox_h(选中)
        
  
        self.vipLogo = UIImageView(frame: CGRect(x: iBtnLeft - 15 - 32, y: conentHeight/2 - 16/2, width: 32, height: 16))
        rowView.addSubview(self.vipLogo)
        self.vipLogo.image = "qianli_member_svip".qianli_R_image
        self.vipLogo.isHidden = true
        
        let iLeft = 15.0 + 48.0 + 10.0
        self.labelTitle.frame = CGRectMake(iLeft, conentHeight/2 - 32/2, rowView.bounds.size.width - iLeft - btnWidth - 10, 18)
        rowView.addSubview(self.labelTitle)
        self.labelTitle.textColor =  .label
        self.labelTitle.font = .systemFont(ofSize: 15)
        self.labelTitle.text = ""
        
        self.labelDelaytime = UILabel(frame: CGRectMake(iLeft, conentHeight/2 - 32/2 + 19, rowView.bounds.size.width - iLeft - btnWidth - 10, 15))
        rowView.addSubview(self.labelDelaytime)
        self.labelDelaytime.textColor =  .secondaryLabel
        self.labelDelaytime.font = .systemFont(ofSize: 10)
        self.labelDelaytime.text = "SHADOWSOCKS"
    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
////        self.checkImgView.frame = CGRect(x: bounds.size.width - 30 - 32 - 10, y: 10, width: 32, height: 32)
////        let iLeft = 15.0 + 48.0 + 10.0
////        self.titleLabel.frame = CGRectMake(iLeft, 0, bounds.size.width - iLeft - 32 - 10, 60)
//    }

    func jHrPakdnRCqvmmiy(title:String){
//解析数据
        if LoginViewModel.shared.nodehealthInfo.count>0,let delaytime = LoginViewModel.shared.nodehealthInfo[title]{
//            let newTitle = "\(title)  |  \(delaytime)"
//            let myMutableString = NSMutableAttributedString(
//                        string: newTitle,
//                        attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15),
//                                     NSAttributedString.Key.foregroundColor:UIColor.secondaryLabel])
//            let range_N:NSRange = (newTitle as NSString).range(of:title)
//            myMutableString.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.label], range: range_N)
//            self.labelTitle.attributedText = myMutableString
            if delaytime == 65535{
                self.labelDelaytime.text = "SHADOWSOCKS | N/A"
            }else{
                self.labelDelaytime.text = "SHADOWSOCKS | \(delaytime) ms"
            }
        }else{
            self.labelDelaytime.text = "SHADOWSOCKS"
        }
 
        self.labelTitle.text = title
        self.labelTitle.textColor = .label

        self.vipLogo.isHidden = true
        let memberLever = Defaults[\.memberLevel]
        if memberLever != 2 && !LoginViewModel.shared.subscribeNodes.contains(title){
            self.labelTitle.textColor = .secondaryLabel
            if 1==memberLever{
                self.vipLogo.isHidden = false
            }
        }
        if(title.contains(LocalizedString("Auto Best Router"))){
            self.vipLogo.isHidden = true
            self.labelTitle.textColor = .label
        }
        
        R7mWnyyQMCHDghh4(title)
        
        self.checkImgView.image = "qianli_checkbox_n".qianli_R_image
        
        var selectTitle = Qianli_G_SharedMdb.shared.GetNodeName()
        if let lastNode = Qianli_G_SharedMdb.shared.lastQianliNodeTitle{
            if !lastNode.isEmpty{
                selectTitle = lastNode
            }
        }
        
        if self.DvET4H5IGpMq3MCT(selectTitle){
            if(title.contains(LocalizedString("Auto Best Router"))){
                self.checkImgView.image = "qianli_checkbox_h".qianli_R_image
                self.labelTitle.textColor = UIColor(hexString: "#3964ff")
            }
        }else if(selectTitle == title){
            self.checkImgView.image = "qianli_checkbox_h".qianli_R_image
            self.labelTitle.textColor = UIColor(hexString: "#3964ff")
        }
    }
    
    private func DvET4H5IGpMq3MCT(_ title:String)->Bool{
        return (title.isEmpty || title == "Qianli.Ai.Router")
    }
    
    func j5WvUB9cdSjK5lw2(_ select:Bool){
//点击了该cell，更新选中状态
        if(select){
            self.checkImgView.image = "qianli_checkbox_h".qianli_R_image
            if let title = self.labelTitle.text{
                if(title.contains(LocalizedString("Auto Best Router"))){
                    Qianli_G_SharedMdb.shared.SetNodeName(nodeName: "Qianli.Ai.Router")
                }else{
                    Qianli_G_SharedMdb.shared.SetNodeName(nodeName: title)
                }
                self.labelTitle.textColor = UIColor(hexString: "#3964ff")
            }
        }else{
            self.checkImgView.image = "qianli_checkbox_n".qianli_R_image
            self.labelTitle.textColor = .label
            if let title = self.labelTitle.text{
                if(title.contains(LocalizedString("Auto Best Router"))){
                    self.labelTitle.textColor = .label
                }else{
                    let memberLever = Defaults[\.memberLevel]
                    if memberLever != 2 && !LoginViewModel.shared.subscribeNodes.contains(title){
                        self.labelTitle.textColor = .secondaryLabel
                    }
                }
                
            }
        }
        
    }
    
    func R7mWnyyQMCHDghh4(_ title:String){
        if(title.contains("巴西")){
            self.icoLogo.image = "qianli_flag_bx".qianli_R_image
        }
        else if(title.contains("香港")){
            self.icoLogo.image = "qianli_flag_xg".qianli_R_image
        }
        else if(title.contains("德国")){
            self.icoLogo.image = "qianli_flag_dg".qianli_R_image
        }
        else if(title.contains("俄罗斯")){
            self.icoLogo.image = "qianli_flag_els".qianli_R_image
        }
        else if(title.contains("法国")){
            self.icoLogo.image = "qianli_flag_fg".qianli_R_image
        }
        else if(title.contains("韩国")){
            self.icoLogo.image = "qianli_flag_hg".qianli_R_image
        }
        else if(title.contains("美国")){
            self.icoLogo.image = "qianli_flag_mg".qianli_R_image
        }
        else if(title.contains("墨西哥")){
            self.icoLogo.image = "qianli_flag_mxg".qianli_R_image
        }
        else if(title.contains("日本")){
            self.icoLogo.image = "qianli_flag_rb".qianli_R_image
        }
        else if(title.contains("新加坡")){
            self.icoLogo.image = "qianli_flag_xjp".qianli_R_image
        }
        else if(title.contains("印度")){
            self.icoLogo.image = "qianli_flag_yd".qianli_R_image
        }
        else if(title.contains("英国")){
            self.icoLogo.image = "qianli_flag_yg".qianli_R_image
        }
        else if(title.contains("英国")){
            self.icoLogo.image = "qianli_flag_yg".qianli_R_image
        }
        else if(title.contains("巴基斯坦")){
            self.icoLogo.image = "qianli_flag_bjst".qianli_R_image
        }
        else if(title.contains("迪拜")){
            self.icoLogo.image = "qianli_flag_db".qianli_R_image
        }
        else if(title.contains("菲律宾")){
            self.icoLogo.image = "qianli_flag_flb".qianli_R_image
        }
        else if(title.contains("马来西亚")){
            self.icoLogo.image = "qianli_flag_mlxy".qianli_R_image
        }
        else if(title.contains("泰国")){
            self.icoLogo.image = "qianli_flag_tg".qianli_R_image
        }
        else if(title.contains("西班牙")){
            self.icoLogo.image = "qianli_flag_xby".qianli_R_image
        }
        else if(title.contains("意大利")){
            self.icoLogo.image = "qianli_flag_ydl".qianli_R_image
        }
        else if(title.contains("印尼") || title.contains("印度尼西亚")){
            self.icoLogo.image = "qianli_flag_yn".qianli_R_image
        }
        else if(title.contains("台湾")){
            self.icoLogo.image = "qianli_flag_tw".qianli_R_image
        }
        else if(title.contains(LocalizedString("Auto Best Router"))){
            self.icoLogo.image = "qianli_flag_auto".qianli_R_image
        }
        else{
            self.icoLogo.image = "qianli_flag_default".qianli_R_image
        }
    }
    
}
